//
//  UIImage+Scale.m
//  SmartHomeSDK
//
//  Created by haimen_ios_imac on 2017/6/7.
//  Copyright © 2017年 深圳海曼科技有限公司. All rights reserved.
//

#import "UIImage+Scale.h"

NSString *const app_name = @"速通交易APP";
@implementation UIImage (Scale)

- (UIImage *)scaleToSize:(CGSize)size
{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [self drawInRect:CGRectMake(0,0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    //返回新的改变大小后的图片
    return scaledImage;
}

static CGFloat www = 200.0f;
- (UIImage *)turnToIconImage
{
    CGSize newSize = CGSizeMake(www, www);
    if (self.size.width) {
        newSize = CGSizeMake(www, www*(self.size.height/self.size.width));
    }
    UIImage *image = [self scaleToSize:newSize];
    UIImageJPEGRepresentation(image, .1f);
    return image;
   
}

static CGFloat businessWWW = 800.0f;
- (UIImage *)turnToBusinessImage
{
    CGSize newSize = CGSizeMake(businessWWW, businessWWW);
    if (self.size.width && self.size.width > businessWWW) {
        
        newSize = CGSizeMake(businessWWW, businessWWW*(self.size.height/self.size.width));
    }
    UIImage *image = [self scaleToSize:newSize];
    UIImage *appNameImage = [image watermarkImage:image withName:app_name];
    UIImageJPEGRepresentation(appNameImage, .1f);
    return appNameImage;
}

- (UIImage *)watermarkImage:(UIImage *)img withName:(NSString *)name
{
    NSString* mark = name;
    int w = img.size.width;
    int h = img.size.height;
    UIGraphicsBeginImageContext(img.size);
    [img drawInRect:CGRectMake(0, 0, w, h)];
    NSDictionary *attr = @{
                           NSFontAttributeName: [UIFont boldSystemFontOfSize:30],  //设置字体
                           NSForegroundColorAttributeName : [UIColor whiteColor]   //设置字体颜色
                           };
    CGSize markSize = [mark sizeWithAttributes:attr];
    [mark drawInRect:CGRectMake(w -markSize.width, h-markSize.height ,markSize.width ,markSize.height ) withAttributes:attr];  //右下角
    UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return aimg;
}


@end
